-- 功能面板，用于展示更新日志，游戏时长，
local key_board = GetModConfigData("sw_mainboard")
if not key_board then return end

local TEMPLATES = require "widgets/redux/templates"
local TextBtn = require "widgets/textbutton"
local ImageButton = require "widgets/imagebutton"
local HuxiWindow = require "widgets/huxi_enter_window"

local POSDATA = "Pos"		-- 存储四个数据 位置(X,Y), 贴图(altas,tex)
local TEXDATA = "Tex"
local SaveSHide = "butterfly"
local SHide = false



AddClassPostConstruct("screens/redux/multiplayermainscreen", function(self)
	local texdata = LoadModData(TEXDATA)
	local posdata = LoadModData(POSDATA)
	SHide = LoadModData(SaveSHide)
	local length_img = 70
	local tex = texdata and texdata.tex or "modicon.tex"
	local altas = texdata and texdata.altas or "modicon.xml"
	local screen_w, screen_h = GLOBAL.TheSim:GetScreenSize()
	local D_x,D_y= screen_w*1750/1920,screen_h*50/1080
	local posX = posdata and posdata.posX or D_x
	local posY = posdata and posdata.posY or D_y
	local function SetFixedImg(img, length_img, tex, altas)
		if not altas then
			altas = GLOBAL.GetInventoryItemAtlas(tex)
		end
		if not altas or not GLOBAL.TheSim:AtlasContains(altas, tex) then
			altas,tex = "modicon.xml", "modicon.tex"
		end
		img:SetTextures(altas, tex)
		local sizeX, sizeY = img:GetSize()
		local trans_scale = math.min(length_img / sizeX, length_img / sizeY)
		img:SetNormalScale(trans_scale)
		img:SetFocusScale(trans_scale * 1.2)
		
		SaveModData(TEXDATA, {
			altas = altas,
			tex = tex,
		})
	end
	local function GetRandomImg()
		if GLOBAL.PREFAB_SKINS then
			local filters = GLOBAL.GetRandomItem(GLOBAL.PREFAB_SKINS)
			if type(filters) == "table" then
				local tex = GLOBAL.GetRandomItem(filters)
				if type(tex) == "string" then
					tex = tex..".tex"
					local atlas = GLOBAL.GetInventoryItemAtlas(tex)
					if atlas and GLOBAL.TheSim:AtlasContains(atlas, tex) then
						return tex
					end
				end
			end
		end
	end
	local function SetRandomImg(img, oldtex)
		local flag = true
		local newtex
		while flag do
			newtex = GetRandomImg()
			if newtex and newtex ~= oldtex then
				flag = false
			end
		end
		SetFixedImg(img, length_img, newtex)
	end

    local function ToggleShowBoard()
        if self.huxiboard then
            if self.huxiboard:IsVisible() then
                self.huxiboard:Hide()
            else
                self.huxiboard:Show()
            end
        end
    end
	modimport("info/lastlog.lua")
    self.huxiboard = self.fixed_root:AddChild(HuxiWindow(modname))
    self.lIcon = self.fixed_root:AddChild(ImageButton())
    local img = self.lIcon
	SetFixedImg(img, length_img, tex, altas)
	img:SetVAnchor(GLOBAL.ANCHOR_BOTTOM)    --设置原点到屏幕下方
	img:SetHAnchor(GLOBAL.ANCHOR_LEFT)        --设置原点到屏幕左方，综合起来就是左下方
	img:SetPosition(posX, posY)

    local lasttime = 0
	img.FollowMouse = function(self)
		if self.followhandler == nil then
			self.followhandler = GLOBAL.TheInput:AddMoveHandler(function(x, y) 
				self:UpdatePosition(x,y)
			end)
		end
	end
	img.OnMouseButton = function(_self, button, down, x, y)    --注意:此处应将self.drag_button替换为你要拖拽的widget
		if button == GLOBAL.MOUSEBUTTON_LEFT and down then    --鼠标中键按下
			lasttime = GLOBAL.GetTime()
			return _self:FollowMouse()
		elseif button == GLOBAL.MOUSEBUTTON_LEFT and not down then            --鼠标中键抬起
			if GLOBAL.GetTime()-lasttime < 0.2 then
                -- 点击事件
                ToggleShowBoard()
			else
				SaveModData(POSDATA, {
					posX = x,
					posY = y,
				})
			end
		end
		_self:UpdatePosition(x, y)
		_self:StopFollowMouse()
	end
	img.OnControl = LROnControl
    img:SetOnClick(function(lmb)
		if not lmb then SetRandomImg(img, tex) end
	end)
	img:MoveToFront()
	if SHide then
		img:Hide()
	else
		img:Show()
	end

    if type(key_board) == "number" then
        GLOBAL.TheInput:AddKeyUpHandler(key_board, function ()
            ToggleShowBoard()
        end)
    end
end)